package com.lhkj.ct.base.validator.constraints;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Documented
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE })
@Retention(RUNTIME)
@Constraint(validatedBy = {HasNonElement.Checker.class})
public @interface HasNonElement {

    String message() default "{org.hibernate.validator.constraints.NotEmpty.message}";

    Class<?>[] groups() default { };

    Class<? extends Payload>[] payload() default { };

    class Checker implements ConstraintValidator<HasNonElement, Iterable<?>> {

        @Override
        public void initialize(HasNonElement annotation) {}

        @Override
        public boolean isValid(Iterable value, ConstraintValidatorContext context) {
            if (value == null || !value.iterator().hasNext()) {
                return true;
            }
            for (Object o : value) {
                if (o == null) {
                    return false;
                }
            }
            return true;
        }
    }
}
